<?php
class  Report extends Basic {
	
    /**
    * 
    * Description 查询条件控制
    * @param field_type  bare_field_name
    * @param Kohana_Database_Query $pre
    * @param array $condition 查询条件
    * @param string $flag  为true就执行排序 
    * @param String $tablename 表名
    * @return Kohana_Database_Query 
    */
   public static function getListInfoCondition($pre,$condition,$flag=true,$tablename) {
	   if (isset($condition['enterprise_dominant']) && $condition['enterprise_dominant'] != -1) {
		   $condition1 = array(
			   'enterprise_dominant',
			   '=',
			   trim($condition['enterprise_dominant'])
		   );
		   $pre = self::condition($pre,$condition1);
	   }
	   if(isset($condition['export_tax_rebate'])&&$condition['export_tax_rebate']!=-1){
		   $condition1 = array(
			   'export_tax_rebate',
			   '=',
			   trim($condition['export_tax_rebate'])
		   );
		   $pre        = self::condition($pre,$condition1);
	   }
        if(isset($condition['sku'])&&!empty($condition['sku'])){
            $condition1=array('sku','=',trim($condition['sku']));
            $pre=self::condition($pre,$condition1);
        }
       if(isset($condition['order_id'])&&!empty($condition['order_id'])){
           $condition1=array('order_id','=',trim($condition['order_id']));
           $pre=self::condition($pre,$condition1);
       }
    if(isset($condition['supplier'])&&!empty($condition['supplier'])) {
    	$condition['supplier']=Report::getidByName($condition['supplier']);
    	$condition1=array('supplier_id','=',$condition['supplier']);
    	$pre=self::condition($pre,$condition1);
    }
   if($tablename=='warehouseorders' && isset($condition['batch_code']) && !empty($condition['batch_code'])) {
       $condition1=array('batch_code', '=', addslashes($condition['batch_code']));
       $pre=self::condition($pre,$condition1);
   }
       if(isset($condition['warehouseid']) && $condition['warehouseid'] != -1) {
           $condition1=array('warehouseid', '=', addslashes($condition['warehouseid']));
           $pre=self::condition($pre,$condition1);
       }
    if(isset($condition['username'])&&!empty($condition['username'])){
    	$user_id =Erpuser::lookup_by_nick (trim($condition['username']));
    	
    	if($tablename=='warehouseorders'){
    			$condition1=array('warehouse_man','=',$user_id);
    	}else {
    			$condition1=array('delivery_man','=',$user_id);
    	}
			$pre=self::condition($pre,$condition1);
    }
    
    if(isset($condition['start_time'])&&!empty($condition['start_time'])){
    	
    	if($tablename=='warehouseorders'){
    		$condition1=array('warehouseorders.warehouse_date','>=',$condition['start_time']);
    	}else {
    		$condition1=array('deliveryorders.delivery_date','>=',$condition['start_time']);
    	}
    	$pre=self::condition($pre,$condition1);
    }
    if(isset($condition['end_time'])&&!empty($condition['end_time'])){
//     	$time=date('Y-m-d', strtotime ("+1 day", strtotime($condition['end_time'])));
    	if($tablename=='warehouseorders'){
    		$condition1=array('warehouseorders.warehouse_date','<=',$condition['end_time']);
    	}else {
    		$condition1=array('deliveryorders.delivery_date','<=',$condition['end_time']);
    	}
    	$pre=self::condition($pre,$condition1);
    }
    if(isset($condition['type'])&&!empty($condition['type'])){
    	$condition1=array('type','=',$condition['type']);
    	$pre=self::condition($pre,$condition1);
    }
    if($flag){
    	if(isset($condition['sortcondition'])&&!empty($condition['sortcondition'])){
    		if($tablename=='warehouseorders'){
    			$time="warehouse_date";
    		}else{
    			$time="delivery_date";
    		}
        	switch ($condition['sortcondition'])
				{
					case 1:
						$value='sku';
						break;
					case 2:
						$value='supplier_id';
						break;
					case 3:
					$value=$time;
					break;
				}
    		$sort=$condition['order']==1?'ASC':'DESC';
    		$pre=$pre->order_by($value,$sort );
    	}
    }
    return $pre;
   }
   
   
   public  static function getcountListInfoCondition($pre,$condition,$flag=true,$tablename){

	   if(isset($condition['enterprise_dominant'])&&$condition['enterprise_dominant']!=-1){

		   if($tablename=='warehouseorders'){
			   $condition1 = array(
				   'warehouseorders.enterprise_dominant',
				   '=',
				   $condition['enterprise_dominant']
			   );
		   }
		   $pre = self::condition($pre,$condition1);
	   }
	   if(isset($condition['export_tax_rebate'])&&$condition['export_tax_rebate']!=-1){

		   if($tablename=='warehouseorders'){
			   $condition1 = array(
				   'warehouseorders.export_tax_rebate',
				   '=',
				   $condition['export_tax_rebate']
			   );
		   }
		   $pre = self::condition($pre,$condition1);
	   }
   	if(isset($condition['start_time'])&&!empty($condition['start_time'])){
   		 
   		if($tablename=='warehouseorders'){
   			$condition1=array('warehouseorders.warehouse_date','>=',$condition['start_time']);
   		}else {
   			$condition1=array('deliveryorders.delivery_date','>=',$condition['start_time']);
   		}
   		$pre=self::condition($pre,$condition1);
   	}
   	
   	if(isset($condition['end_time'])&&!empty($condition['end_time'])){
   		//     	$time=date('Y-m-d', strtotime ("+1 day", strtotime($condition['end_time'])));
   		if($tablename=='warehouseorders'){
   			$condition1=array('warehouseorders.warehouse_date','<=',$condition['end_time']);
   		}else {
   			$condition1=array('deliveryorders.delivery_date','<=',$condition['end_time']);
   		}
   		$pre=self::condition($pre,$condition1);
   	}
   	
    if(isset($condition['type'])&&!empty($condition['type'])){
    	$condition1=array('type','=',$condition['type']);
    	$pre=self::condition($pre,$condition1);
    }
    
    if($tablename=='warehouseorders'){
    		if(isset($condition['group'])&&!empty($condition['group'])){
    			switch ($condition['group'])
    			{
    				case 1:
    					$pre=$pre->group_by('sku');
    					break;
    				case 2:
    					$pre=$pre->group_by('supplier_id');
    					break;
    				case 3:
    					$pre=$pre->group_by('sku');
    					$pre=$pre->group_by('supplier_id');
    					break;
    			}
    		}
    }else{
    	$pre=$pre->group_by('sku');
    }

   	if(isset($condition['sku'])&&$condition['sku']!=''){
   		$condition1=array('sku','=',trim($condition['sku']));
   		$pre=self::condition1($pre,$condition1);
   	}
   	
     if(isset($condition['supplier'])&&!empty($condition['supplier']))
    {
    	$condition['supplier']=Report::getidByName($condition['supplier']);
    	$condition1=array('supplier_id','=',$condition['supplier']);
    	$pre=self::condition1($pre,$condition1);
    }

   	if($flag){
   	  if(isset($condition['sortcondition'])&&!empty($condition['sortcondition'])){
	   	switch ($condition['sortcondition'])
	   	{
	   		case 1:
	   			$value='sku';
	   			break;
	   		case 2:
	   			$value='single_price';
	   			break;
	   		case 3:
	   			$value='money';
	   			break;
	   		case 4:
	   			$value='cost';
	   			break;
	   		case 5:
	   			$value='quantity';
	   			break;
	   	   }
   	    }else{
   	    	   $value='sku';
   	    }
   	    $sort=$condition['order']==1?'ASC':'DESC';
   	    $pre=$pre-> order_by($value,$sort );
   	}
    return $pre;
   	}
   	
   
 /**
  * 
  * Description 获取入库单的种类
  * @param field_type  bare_field_name
  * @param int $id
  * @return string  
  */
   public static function  getIntpyeByid($id)
   {
   	/*switch ($id) {

   		case 10:
   			return '正常采购入库';
   			break;
   		case 20:
   			return '订单销售退回（退件入库）';
   			break;
   		case 30:
   			return '盘赢';
   			break;
   		case 40:
   			return '供应商礼物';
   			break;
   		case 50:
   			return '样品归还';
   			break;
   	}*/
							$st = ORM::factory('statusdic')->where('table','=','warehouseorders')->where('number','=',$id)->find();
							if($st->id!=''){
								return $st->value;
							}else{
								return 0;
							}
   }
   /**
    *
    * Description 获取出库单的种类
    * @param field_type  bare_field_name
    * @param int $id
    * @return string
    */
   public static function  getOuttpyeByid($id)
   {/*
   	switch ($id) {
   		case 10:
   			return '正常销售出库';
   			break;
   		case 20:
   			return '盘亏';
   			break;
    	case 30:
   			return '礼物送人';
   			break;
    	 case 40:
   			return '样品临时借出';
   			break;
    	 case 50:
    	 	return '报废（进废品仓）';
    	 	break;
		case 60:
			return '淘宝销售出库';
			break;
		case 70:
			return '其他';
			break;
		case 65:
			return '海外仓';
			break;
   	}*/
							$st = ORM::factory('statusdic')->where('table','=','deliveryorders')->where('number','=',$id)->find();
							if($st->id!=''){
								return $st->value;
							}else{
								return 0;
							}

   }
}
